﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_LOAIKHACH
    {
        private static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //thêm
        public static bool ThemLKH(ref string err,string MaLoaiKH,string TenLoai)
        {
            bool them = false;
            try
            {
                LOAIKHACH lk = new LOAIKHACH();
                lk.MaLoaiKH = MaLoaiKH;
                lk.TenLoai = TenLoai;
                lk.DaXoa = false;
                db.LOAIKHACHes.InsertOnSubmit(lk);
                db.SubmitChanges();
                them = true;
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;

        }
        //cập nhật
        public static bool CapNhatLK(ref string err,string MaLoaiKH,string TenLoai)
        {
            bool update = false;
            try
            {
                LOAIKHACH tim = db.LOAIKHACHes.Single(lk => lk.MaLoaiKH.Equals(MaLoaiKH));
                if(tim!=null)
                {
                    tim.MaLoaiKH = MaLoaiKH;
                    tim.TenLoai = TenLoai;
                    db.SubmitChanges();
                    update = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //xóa
        public static bool XoaLK(ref string err,string MaLoaiKH)
        {
            bool xoa = false;
            try
            {
                LOAIKHACH tim = db.LOAIKHACHes.Single(lk => lk.MaLoaiKH.Equals(MaLoaiKH));
                if(tim!=null)
                {
                    db.LOAIKHACHes.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;

                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }
        //đánh dấu xóa
        public static bool DanhDauXoa(ref string err,string MaLoaiKH,bool value)
        {
            bool danhdauxoa = false;
            try
            {
                LOAIKHACH tim = db.LOAIKHACHes.Single(lk => lk.MaLoaiKH.Equals(MaLoaiKH));
                if (tim != null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }

            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }
        //ConverToDataTable
        public static DataTable ConverToDataTable(List<LOAIKHACH> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaLoaiKH");
            dt.Columns.Add("TenLoai");
            //dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaLoaiKH, i.TenLoai);
            }
            return dt;
        }
        //Lấy thông tin Loại khách
        public static DataTable LayThpngTinLK()
        {
            var ttlk = from lk in db.LOAIKHACHes where lk.DaXoa==false select lk;
            return ConverToDataTable(ttlk.ToList());
        }
        //lấy lịch sử xóa loại khách
        public static DataTable LayLichSuLK()
        {
            var ttlk = from lk in db.LOAIKHACHes where lk.DaXoa == true select lk;
            return ConverToDataTable(ttlk.ToList());
        }
    }
}
